<?php

namespace common\modules\sms\models;

use common\behaviors\TimestampBehavior;
use common\components\ActiveRecord;
use Yii;
use yii\db\ActiveQuery;
use yii\db\Expression;

/**
 * This is the model class for table "sms_event".
 *
 * @property integer $id
 * @property string $service_id
 * @property integer $customer_id
 * @property ActiveQuery|Customer $customer
 * @property string $mobile
 * @property string $content
 * @property string $server_ip
 * @property string $client_ip
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 */
class Event extends ActiveRecord
{

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sms_event';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['mobile', 'content', 'client_ip'], 'required'],
            [['customer_id', 'status', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
            [['service_id', 'content'], 'safe'],
            [['server_ip', 'client_ip'], 'safe'],
            [['mobile'], 'string', 'max' => 32]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app/sms', 'ID'),
            'service_id' => Yii::t('app/sms', 'Service ID'),
            'customer_id' => Yii::t('app/sms', 'Customer ID'),
            'mobile' => Yii::t('app/sms', 'Mobile'),
            'content' => Yii::t('app/sms', 'Content'),
            'status' => Yii::t('app/sms', 'Status'),
            'created_at' => Yii::t('app/sms', 'Created At'),
            'updated_at' => Yii::t('app/sms', 'Updated At'),
            'created_by' => Yii::t('app/sms', 'Created By'),
            'updated_by' => Yii::t('app/sms', 'Updated By'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new Expression('NOW()'),
        ];
        
        return $behaviors;
    }

    /**
     * @return ActiveQuery|Customer
     */
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), [
            'id' => 'customer_id',
        ]);
    }
}
